﻿namespace Rsdn.Janus.VirtualForums.NewMessages
{
    using System;
    using System.Diagnostics;
    using Database;
    using JanusLibAing;
    using MultiLang;
    using SmartApp;

    internal class NewMessagesFeature : VirtualForum
    {
        public NewMessagesFeature(IServiceProvider provider)
            : base(provider, null)
        {
            IIncomingFolderUpdate folderUpdate = (IIncomingFolderUpdate)provider.GetService(typeof(IIncomingFolderUpdate));
            folderUpdate.AddUpdate(this);
            IForumQueryService forumQueryService = ServiceProvider.GetRequiredService<IForumQueryService>();
            DateTime lastUpdateDate = forumQueryService.LastUpdateDate;
            IFormatMessageHelperService formatMessageService =
                ServiceProvider.GetRequiredService<IFormatMessageHelperService>();

            string dateTimeAsString = formatMessageService.GetDateAsString(lastUpdateDate);

            Name = string.Format(ml.ml_string(363, "Новые сообщения c {0}"), dateTimeAsString);
            ID = Forums.VirtualForumNewMessages;
        }

        /// <summary>
        /// Loads the counters only.
        /// </summary>
        /// <param name="id">The forum id.</param>
        internal override void LoadCounters(int id)
        {
            NewMessage_Message root = new NewMessage_Message(ServiceProvider);
            using (new WaitCursor())
            {
                root.FillCounters();
            }
#if FRM_UPDATE_TRACE
            Trace.WriteLine("FRU: NewMessages.LoadCounters");         //MLHIDE
#endif
            MessagesCount = root.RepliesCount;
            Unread = root.UnreadCount;
            RepliesToMeUnread = root.UnreadCount;
        }

        /// <summary>
        /// Loads all topics as children in message.
        /// </summary>
        /// <param name="isLoadAll">if set to <c>true</c> [is load all].</param>
        /// <returns>root message</returns>
        protected override IMsg LoadTopicsAsChildren(bool isLoadAll)
        {
            NewMessage_Message root = new NewMessage_Message(ServiceProvider);
            using (new WaitCursor())
            {
                root.FillChildrenWithMessagesToMe();
#if FRM_UPDATE_TRACE
                Trace.WriteLine(
                    string.Format(
                        "FRM:NewMessages:RepliesToMe Unread {0}, Unread {1}, summary {2}", //MLHIDE
                        root.RepliesToMeUnread,
                        root.Unread,
                        root.Count));
#endif
                MessagesCount = root.Count;
                Unread = root.Unread;
                RepliesToMeUnread = root.Unread; 
            }

            return root;
        }

        /// <summary>
        /// Determines whether [is update need] [the specified MSG in].
        /// </summary>
        /// <param name="msgIn">The MSG in.</param>
        /// <returns>
        ///   <c>true</c> if [is update need] [the specified MSG in]; otherwise, <c>false</c>.
        /// </returns>
        public override bool IsUpdateNeed(object msgIn)
        {
            if (msgIn is Msg)
            {
                Msg msg = msgIn as Msg;
            }

            // It was wish to check if msg included in current forum but forum implmented in an other way
            return true;
        }
    }
}
